#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
挖矿模拟器：摸鱼版
主程序入口
"""

import tkinter as tk
from tkinter import ttk, messagebox
import sys
import os

# 添加项目路径到系统路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from game_core import GameCore
from ui.main_window import MainWindow
from ui.floating_window import FloatingWindow
from utils.config import Config

class MiningSimulator:
    def __init__(self):
        self.config = Config()
        self.game_core = GameCore()
        self.main_window = None
        self.floating_window = None
        
    def start(self):
        """启动游戏"""
        # 创建主窗口
        root = tk.Tk()
        root.withdraw()  # 隐藏根窗口
        
        # 初始化主界面
        self.main_window = MainWindow(root, self.game_core, self)
        
        # 加载存档
        self.game_core.load_game()
        
        # 显示主窗口
        self.main_window.show()
        
        # 启动主循环
        root.mainloop()
    
    def toggle_floating_mode(self):
        """切换浮窗模式"""
        if self.floating_window is None:
            # 创建浮窗
            self.floating_window = FloatingWindow(self.game_core, self)
            # 隐藏主窗口
            self.main_window.hide()
        else:
            # 销毁浮窗，显示主窗口
            self.floating_window.destroy()
            self.floating_window = None
            self.main_window.show()
    
    def quit_game(self):
        """退出游戏"""
        self.game_core.save_game()
        if self.main_window:
            self.main_window.quit()

if __name__ == "__main__":
    try:
        app = MiningSimulator()
        app.start()
    except Exception as e:
        messagebox.showerror("错误", f"程序启动失败: {str(e)}")
        sys.exit(1)